package tripx.siteget.crawler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import tripx.siteget.start.Client;
import tripx.update.Food;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlHeading3;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlParagraph;

public class FoodCrawler {

	ArrayList<Food> foodList;
	// for crawling
	WebClient client;
	// tell the page address
	String url;

	String cityName;

	public FoodCrawler(String cityName, String citySpell) {
		this.cityName = cityName;
		this.url = "http://lvyou.baidu.com/" + citySpell + "/meishi";
		client = new WebClient(BrowserVersion.FIREFOX_17);
		foodList = new ArrayList<Food>();
	}

	public void crawl() {
		try {
			HtmlPage page = client.getPage(url);

			List<?> items = page
					.getByXPath("//*[@id=\"body\"]/div/section/article/div/"
							+ "article/div/div[2]/div[@class=\"sub-busi-mod-list clearfix\"]");
			System.out.println(items.size());
			for (int i = 0; i < items.size(); i++) {
				HtmlDivision div = (HtmlDivision) items.get(i);
				String urlImage = ((HtmlImage) div.getByXPath(
						"./div[@class=\"container clearfix\"]/img").get(0))
						.getSrcAttribute();
				String foodName = ((HtmlHeading3) div
						.getByXPath(
								"./div[@class=\"container clearfix\"]/h3[@itemprop=\"name\"]")
						.get(0)).getTextContent();
				String intro = ((HtmlParagraph) div
						.getByXPath(
								"./div[@class=\"container clearfix\"]/p[@class=\"text-p text-desc-p\"]")
						.get(0)).getTextContent().trim();
				Client.getInstance().uploadFoodImage(urlImage, cityName,
						foodName);
				foodList.add(new Food(foodName, cityName, intro));
			}
		} catch (FailingHttpStatusCodeException | IOException e) {

		}
	}

	public ArrayList<Food> getFoodList() {
		return foodList;
	}

}
